﻿angular.module('Stackoverflow').controller('QuestionItemController', function ($scope, Restangular, $state, $stateParams, $location, Account) {
    'use strict';

    $scope.model = {
        title: 'Title',
        content: 'Content',
        userId: Account
    };

    if ($stateParams.id) {
        Restangular.one('question', $stateParams.id).get().then(function (model) {
            $scope.model = model;
        });
    }

    if ($stateParams.model) {
        $scope.model = JSON.parse($stateParams.model);
    }

    $scope.save = function (model) {
        model.userId = Account;
        return Restangular.all('question').post(model).then(function (id) {
            $state.go('questions');
        });
    };

    $scope.update = function (model) {
        return model.put().then(function () {
            $state.forceReload();
        });
    };

    $scope.remove = function (model) {
        return model.remove({ id: model.id }).then(function () {
            $state.go('questions');
        });
    };

});